<?
/***********************************************************/
/* Class defining the remotetree
/* use web services to open the files
/* by Pablo Dall'Oglio 2001-2006
/***********************************************************/
class RemoteTree extends GtkCTree
{
    /***********************************************************/
    /* Constructor Method
    /***********************************************************/
    function RemoteTree()
    {
        parent::__construct(1, 0, array(_a('Remote Editing Server')));
        parent::set_selection_mode(GTK_SELECTION_SINGLE);
        parent::set_line_style(GTK_CTREE_LINES_SOLID);
        parent::connect_simple('tree-select-row', array(&$this, 'Abrir'));
        $this->BotaoOk = new GtkButton;
    }

    /***********************************************************/
    /* Opens the selected node
    /***********************************************************/
    function Abrir()
    {
        $selecao = $this->selection[0];
        $info1 = parent::node_get_row_data($selecao);
        $info2 = split('://', $info1, 2);
        $Server  = $info2[0];
        $Arquivo = $info2[1];

        if ((substr($Arquivo, -1) == '/') && (!$this->opened[$info1]))
        {
            $Arquivo = substr($Arquivo, 0, -1);
            $opened = $this->AbreDiretorio($Server, $Arquivo, $selecao, null);
            if ($opened)
            {
                parent::expand($selecao);
                $this->opened[$info1] = true;
            }
        }
        elseif ((substr($Arquivo, -1) == '/') && ($this->opened[$info1]))
        {
            parent::expand($selecao);
        }
        elseif (substr($Server,0,4)=='home')
        {
            if (!$this->opened[$info1])
            {
                $opened = $this->AbreDiretorio($Arquivo, '.', $selecao);
                if ($opened)
                {
                    $this->opened[$info1] = true;
                    parent::expand($selecao);
                }
            }
            else
            {
                parent::expand($selecao);
            }
            
        }
        else
        {
            $botao = $this->BotaoOk;
            $botao->clicked();
        }
    }

    /***********************************************************/
    /* Removes a node and file
    /***********************************************************/
    function Apagar()
    {
        $selecao = $this->selection[0];
        $Arquivo = GtkCTree::node_get_row_data($selecao);
        if (is_dir($Arquivo))
        {
            include_once 'Dialog.class';
            new Dialog('Sem a??o');
        }
        else
        {
            parent::remove_node($selecao);
            unlink($Arquivo);
        }
    }

    /***********************************************************/
    /* Difine allowed extensions
    /***********************************************************/
    function DefineExtensions($extensions)
    {
        $this->extensions = $extensions;
    }

    /***********************************************************/
    /* Opens a directory, loads it into the tree
    /***********************************************************/
    function AbreDiretorio($Server, $Diretorio, $NodoRaiz)
    {
        global $Pixmaps;
        
        if (!$Server)
        {
            return false;
        }

        if (!$NodoRaiz)
        {
            $entries = $this->entries;
            $entries[] = array($Server, $Diretorio);
            $this->entries = $entries;
        }
        $extensions = $this->extensions;
        
        $ico = $Pixmaps['home'];

        # Define um array de parametros
        $param = array('Directory'=>$Diretorio);

        # Cria objeto cliente SOAP
        $client = new soap_client("http://$Server/phpgtkeditor-server.php");
        
        $filelist= $client->call('dir_list',$param);
        if ($filelist == 'denied')
        {
            new Dialog(_a('Permission denied'));
            return false;
        }
        
        if ($filelist)
        {
            sort($filelist);
            foreach ($filelist as $arquivo)
            {
                $old = $arquivo;
                $arquivo = trim($arquivo);
                $Caminho = "$Diretorio" . '/' . "$arquivo";
                if ($arquivo == '.' || $arquivo == '..')
                    continue;
                
                if (substr($old,0,1) == ' ')
                {
                    $ico1 = $Pixmaps['folder1'];
                    $ico2 = $Pixmaps['folder2'];
                    $NodoPasta = GtkCTree::insert_node(
                    $NodoRaiz, null, array($arquivo), 5,
                    $ico1[0], $ico1[1], $ico2[0], $ico2[1], false, false);
                    
                    //$this->AbreDiretorio($Caminho, $NodoPasta);
                    GtkCTree::node_set_row_data($NodoPasta, "$Server://$Caminho/");
                }
                else
                {
                    $posicao = explode('.', $arquivo);
                    $ext = $posicao[count($posicao)-1];
					$ico = $Pixmaps[$ext];
                    if (!$ico)
                        $ico = $Pixmaps['generic'];
                    $NodoFilho = GtkCTree::insert_node(
                    $NodoRaiz, null , array($arquivo), 5,
                    $ico[0], $ico[1], $ico[0], $ico[1], true, false);
                    
                    parent::node_set_row_data($NodoFilho, "$Server://$Caminho");
                }
            }
        }
        else
        {
            new Dialog(_a('Connection Problems'));
            return false;
        }
        parent::thaw();
        return true;
    }

    /***********************************************************/
    /* Reloads the tree
    /***********************************************************/
    function ReadAgain()
    {
        $this->opened = null;
        parent::clear();
        parent::thaw();
        $entries = $this->entries;
        $this->entries = null;
        
        if ($entries)
        {
            foreach($entries as $entry)
            {
                $this->AbreDiretorio($entry[0], $entry[1], null);
            }
        }
    }

    /***********************************************************/
    /* Clear all the contents
    /***********************************************************/
    function MyClear()
    {
        parent::clear();
        parent::thaw();
        $this->entries = null;
    }

    /***********************************************************/
    /* AddHome
    /***********************************************************/
    function AddHome($Home, $address)
    {
        global $Pixmaps;
		$ico = $Pixmaps['home'];

        $node = GtkCTree::insert_node(
            $NodoNull, null, array(':: ' . $Home), 5,
            $ico[0], $ico[1], $ico[0], $ico[1], false, false);
        parent::node_set_row_data($node, "home-$Home://$address");
        return $node;
    }
}
?>